<?php
/**
 * Product.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 01:44:04
 * @modified    2023-02-14 01:44:04
 *
 */

namespace App\Models;

use App\Models\Product\QrCode;
use App\Models\Product\Type;
use App\Repositories\ProductRepo;

use Carbon\Carbon;

class ProductModel extends BaseModel
{
    protected $table = 'product';

    protected $appends = [
        'create_time_format',
        'update_time_format',
        'warranty_status_code',
        'warranty_status_title',
        'warranty_start_date',
        'warranty_end_date',
        'attachment_url'
    ];

    protected $fillable = [
        'customer_id',
        'type_id',
        'department_id',
        'product_number',
        'name',
        'model',
        'attachment',
        'country_id',
        'country_name',
        'province_id',
        'province_name',
        'city_id',
        'city_name',
        'county_id',
        'county_name',
        'address',
        'install_address',
        'warranty_start',
        'warranty_end',
        'warranty_type',
        'create_user_id',
        'date_buy',
        'date_check',
    ];

    public function getCreateTimeFormatAttribute()
    {
        return Carbon::parse($this->create_time)->toDateTimeString();
    }

    public function getUpdateTimeFormatAttribute()
    {
        return Carbon::parse($this->update_time)->toDateTimeString();
    }

    public function getWarrantyStartDateAttribute()
    {
        return Carbon::parse($this->warranty_start)->toDateString();
    }

    public function getWarrantyEndDateAttribute()
    {
        return Carbon::parse($this->warranty_end)->toDateString();
    }

    public function getWarrantyStatusCodeAttribute()
    {
        $warrantyEnd = Carbon::parse($this->warranty_end)->toDateString();

        if ($warrantyEnd > Carbon::now()->toDateString()) {
            return ProductRepo::IN_WARRANTY_CODE;
        }
        return ProductRepo::OUT_WARRANTY_CODE;
    }

    public function getWarrantyStatusTitleAttribute()
    {
        $warrantyEnd = Carbon::parse($this->warranty_end)->toDateString();
        if ($warrantyEnd > Carbon::now()->toDateString()) {
            return ProductRepo::IN_WARRANTY_TITLE;
        }
        return ProductRepo::OUT_WARRANTY_TITLE;
    }

    public function getAttachmentUrlAttribute()
    {
        $attachment = $this->attachment;
        if (!$attachment) {
            return '';
        }
        if (!is_file(public_path() . '/' . $attachment)) {
            return '';
        }
        return env('APP_URL') . '/' . $attachment;
    }

    public function scopeByNumber($query, $number)
    {
        return $query->where('product_number', $number);
    }

    public function type()
    {
        return $this->belongsTo(Type::class);
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function department()
    {
        return $this->belongsTo(DeptModel::class);
    }

    public function qrcode()
    {
        return $this->hasOne(QrCode::class);
    }
}
